package com.assetmanage.dao.impl;

import com.assetmanage.dao.AssetDao;
import com.assetmanage.model.Asset;
import com.assetmanage.utils.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AssetDaoImpl implements AssetDao {
    @Override
    public List<Asset> getAllAssets() {
        List<Asset> assets = new ArrayList<>();
        String sql = "SELECT * FROM asset";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Asset asset = new Asset();
                asset.setAssetId(rs.getString("asset_id"));
                asset.setAssetName(rs.getString("asset_name"));
                asset.setAssetStatus(rs.getString("asset_status"));
                asset.setDepId(rs.getString("dep_id"));
                asset.setAssetNum(rs.getInt("asset_num"));
                asset.setTotalNum(rs.getInt("total_num"));
                assets.add(asset);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return assets;
    }

    @Override
    public List<Asset> getAssetsByDepartment(String depId) {
        List<Asset> assets = new ArrayList<>();
        String sql = "SELECT * FROM asset WHERE dep_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, depId);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Asset asset = new Asset();
                asset.setAssetId(rs.getString("asset_id"));
                asset.setAssetName(rs.getString("asset_name"));
                asset.setAssetStatus(rs.getString("asset_status"));
                asset.setDepId(rs.getString("dep_id"));
                asset.setAssetNum(rs.getInt("asset_num"));
                asset.setTotalNum(rs.getInt("total_num"));
                assets.add(asset);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return assets;
    }

    @Override
    public Asset getAssetById(String assetId) {
        String sql = "SELECT * FROM asset WHERE asset_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, assetId);
            rs = stmt.executeQuery();
            if (rs.next()) {
                Asset asset = new Asset();
                asset.setAssetId(rs.getString("asset_id"));
                asset.setAssetName(rs.getString("asset_name"));
                asset.setAssetStatus(rs.getString("asset_status"));
                asset.setDepId(rs.getString("dep_id"));
                asset.setAssetNum(rs.getInt("asset_num"));
                asset.setTotalNum(rs.getInt("total_num"));
                return asset;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return null;
    }

    @Override
    public boolean addAsset(Asset asset) {
        String sql = "INSERT INTO asset (asset_id, asset_name, asset_status, dep_id, asset_num, total_num) VALUES (?, ?, ?, ?, ?, ?)";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, asset.getAssetId());
            stmt.setString(2, asset.getAssetName());
            stmt.setString(3, asset.getAssetStatus());
            stmt.setString(4, asset.getDepId());
            stmt.setInt(5, asset.getAssetNum());
            stmt.setInt(6, asset.getTotalNum());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean updateAsset(Asset asset) {
        String sql = "UPDATE asset SET asset_name = ?, asset_status = ?, dep_id = ?, asset_num = ?, total_num = ? WHERE asset_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, asset.getAssetName());
            stmt.setString(2, asset.getAssetStatus());
            stmt.setString(3, asset.getDepId());
            stmt.setInt(4, asset.getAssetNum());
            stmt.setInt(5, asset.getTotalNum());
            stmt.setString(6, asset.getAssetId());
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean deleteAsset(String assetId) {
        String sql = "DELETE FROM asset WHERE asset_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, assetId);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }

    @Override
    public boolean updateAssetStatus(String assetId, int assetNum) {
        String sql = "UPDATE asset SET asset_num = ?, asset_status = CASE " +
                    "WHEN ? = 0 THEN '库存为空' " +
                    "WHEN ? = total_num THEN '全部在库' " +
                    "ELSE '存在库存' END " +
                    "WHERE asset_id = ?";
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, assetNum);
            stmt.setInt(2, assetNum);
            stmt.setInt(3, assetNum);
            stmt.setString(4, assetId);
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }
} 